package cn.panda.edu.teacher;

import cn.panda.edu.model.*;
import cn.panda.edu.service.CourseService;
import cn.panda.edu.service.SysMenuService;
import cn.panda.edu.service.VideoService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.FileUtil;
import com.mysql.fabric.Response;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 */
@Controller
public class WebController {
    @Autowired
    VideoService videoService;
    @Autowired
    SysMenuService sysMenuService;
    @Autowired
    CourseService courseService;

    @GetMapping("/")
    public String index() {
        return "login";
    }

    //退出
    @GetMapping("/loginout")
    public String loginout(HttpSession session) {
        session.removeAttribute(Constant.LOGIN_USER);
        session.removeAttribute("teacher");
        return "login";
    }

    @GetMapping("/home")
    public String home() {
        return "home";
    }

    @GetMapping("/test")
    public String test() {
        return "test";
    }

    @GetMapping("index")
    public String indexTest(HttpSession session, Model model) {
        String name = null;
        String img = null;
        String type = null;
        Integer typeId = 1;
        String index = null;
        Student student = (Student) session.getAttribute(Constant.LOGIN_USER);
        if (student != null) {
            name = student.getStudentName();
            img = student.getStudentPhoto();
            type = "学生";
            index = "/studentIndex";

        } else {
            Teacher teacher = (Teacher) session.getAttribute("teacher");
            if (teacher != null) {
                name = teacher.getTeacherRealname();
                img = teacher.getTeacherPhoto();
                type = "教师";
                typeId = 2;
                index = "/teacherIndex";
            }
        }
        model.addAttribute("index", index);
        List<SysMenu> oneMenuAll = sysMenuService.findList(1, null, typeId);
        List<SysMenu> twoMenuAll = sysMenuService.findList(2, null, typeId);
        model.addAttribute("oneMenuAll", oneMenuAll);
        model.addAttribute("twoMenuAll", twoMenuAll);
        model.addAttribute("name", name);
        model.addAttribute("img", img);
        model.addAttribute("type", type);
        return "index";
    }


    /**
     * 菜单查找
     *
     * @param req
     * @return
     */
    @RequestMapping("menucha")
    public String menucha(HttpSession session, Model model, HttpServletRequest req) {
        String val = null;
        if (!StringUtils.isEmpty(req.getParameter("val"))) {
            val = req.getParameter("val");
        }
        Integer typeId = 1;

        Teacher teacher = (Teacher) session.getAttribute("teacher");
        if (teacher != null) {
            typeId = 2;
        }
        List<SysMenu> oneMenuAll = sysMenuService.findListFather(1, val, typeId);
        List<SysMenu> twoMenuAll = sysMenuService.findList(2, val, typeId);

        model.addAttribute("oneMenuAll", oneMenuAll);
        model.addAttribute("twoMenuAll", twoMenuAll);
        model.addAttribute("val", val);


        return "common/leftlists";

    }

    @GetMapping("/studentIndex")
    public String studentIndex() {
        return "/student/student_index";
    }

    @GetMapping("/teacherIndex")
    public String teacherIndex() {
        return "/teacher/teacher_index";
    }

    /**
     * 教师首页
     *
     * @return
     */
    @GetMapping("/teacher_home")
    public String teacherHome() {
        return "/teacher/teacher_home";
    }


    //课程管理
    @GetMapping("/courseT")
    public String courseT() {
        return "/teacher/course";
    }

    @GetMapping("/courseInform")
    public String courseInform() {
        return "/teacher/courseInform";
    }

    //教学资源
    @GetMapping("/courseware")
    public String courseWare() {
        return "/teacher/courseware";
    }


    //试卷
    @GetMapping("/teacherJudge")
    public String teacherJudge() {
        return "/teacher/c_judge";
    }


    //试卷
    @GetMapping("/judge")
    public String judge(Integer courseId, Model model) {
        model.addAttribute("courseId", courseId);
        return "/teacher/teacher_judge";
    }


    //作业下载
    @GetMapping("/teacherWork")
    public String teacherWork() {
        return "/teacher/teacher_work";
    }

    //考试管理
    @GetMapping("/exam")
    public String exam() {
        return "/teacher/exam";
    }

    //成绩管理
    @GetMapping("/score")
    public String score() {
        return "/teacher/score";
    }

    //章节管理
    @GetMapping("/section")
    public String section(Integer courseId, Model model) {
        model.addAttribute("courseId", courseId);
        Course course = courseService.selectByPrimaryKey(courseId);
        model.addAttribute("name", course.getCourseName());
        model.addAttribute("courseId", courseId);
        return "/teacher/section";
    }

    @GetMapping("/coursePage")
    public String coursePage() {
        return "/student/course_page";
    }

    //视频点播
    @GetMapping("/partInfo")
    public String partInto() {
        return "/student/part_info";
    }

    @GetMapping("/printCard")
    public String printCard() {
        return "/student/print_card";
    }

    @GetMapping("/studentInform")
    public String studentInform() {
        return "/student/student_inform";
    }

    //
    @GetMapping(value = "/download")
    public void download(@RequestParam(value = "url") String url, HttpServletResponse response) throws Exception {
        FileUtil.downloadFile(url, response);
    }


//    @RequestMapping(value = "/download", method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String, Object> downloadFile(HttpServletResponse res,@RequestParam("url") String url) {
//        int status = 200;
//        String msg = null ;
//        String filePath = null ;
//        try {
//            FileUtil.downloadFile(url, "123", res);
//            msg= "下载成功" ;
//        } catch (Exception e1) {
//            status = 500;
//            msg= "下载失败" ;
//            e1.printStackTrace();
//        } // 各浏览器基本都支持ISO编码
//
//        return ResultDataUtil.resData(status, msg, filePath);
//    }

    //资源下载
    @GetMapping("/resourceDownload")
    public String resourceDownload() {
        return "/student/resource_download";
    }

    @GetMapping("/resourceUpload")
    public String resourceUpload() {
        return "/student/resource_upload";
    }

    //章节测试
    @GetMapping("/sectionExam")
    public String sectionExam() {
        return "/student/section_exam";
    }

    @GetMapping("sectionExamQuestion")
    public String sectionExamQuestion() {
        return "/student/section_exam_question";
    }

    //休学管理
    @GetMapping("/stuChange")
    public String stuChange() {
        return "/student/stu_change";
    }

    @GetMapping("/stuChangeFill")
    public String stuChangeFill() {
        return "/student/stu_change_fill";
    }

    //课程评价
    @GetMapping("/courseEvaluate")
    public String courseEvaluate() {
        return "/student/stu_course_evaluate";
    }

    //考试管理
    @GetMapping("stuExam")
    public String cExam() {
        return "/student/c_exam";
    }

    @GetMapping("sexam")
    public String stuExam(Integer courseId, Model model) {
        model.addAttribute("courseId", courseId);
        return "/student/stu_exam_start";
    }

    @GetMapping("stuExamStart")
    public String stuExamStart() {
        return "/student/stu_exam_start";
    }

    @GetMapping("stuGrade")
    public String stuGrade() {
        return "/student/stu_grade";
    }

    //个人信息
    @GetMapping("/stuInfo")
    public String stuInfo() {
        return "/student/stu_info";
    }

    //章节信息
    @GetMapping("/studyCourse")
    public String studyCourse() {
        return "/student/study_course";
    }

    //教师评价
    @GetMapping("/teacherInfo")
    public String teacherInfo() {
        return "/student/teacher_info";
    }

    //教学计划
    @GetMapping("/techPlan")
    public String techPlan() {
        return "/student/tech_plan";
    }

    @GetMapping("video")
    public String video(Integer partId, Model model) {

        if (partId == null) {
            partId = 1;
        }
        Video video = videoService.selectByPart(partId);
        if (video != null) {
            String path = video.getVideoPath();
        }
        model.addAttribute("video", video);
        return "/student/video";
    }

    @GetMapping("play")
    public void play(HttpServletRequest request, HttpServletResponse response, String url) {
        try {
            FileInputStream fis = new FileInputStream(url); // 以byte流的方式打开文件
            int i = fis.available(); //得到文件大小
            byte data[] = new byte[i];
            fis.read(data);  //读数据
            response.setContentType("video/*"); //设置返回的文件类型
            OutputStream toClient = response.getOutputStream(); //得到向客户端输出二进制数据的对象
            toClient.write(data);  //输出数据
            toClient.flush();
            toClient.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件不存在");
        }
    }

    //作业列表 上传作业
    @GetMapping("/workList")
    public String workList() {
        return "/student/student_work_list";
    }

    @GetMapping("/workNotice")
    public String workNotice() {
        return "/student/student_notice";
    }
}
